from selenium.common import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from pom.basePage import BasePage


class Category(BasePage):
    title_input = (By.NAME, 'category.title')
    pid_input = (By.NAME, 'category.pid')
    slug_input = (By.NAME, 'category.slug')
    order_number_input = (By.NAME, 'category.order_number')
    content_input = (By.NAME, 'category.content')
    summary_input = (By.NAME, 'category.summary')
    icon_input = (By.NAME, 'category.icon')
    flag_input = (By.NAME, 'category.flag')
    show_on_menu_input = (By.XPATH, '/html/body/div/div/section[2]/div/div[1]/div/form/div[1]/div[9]/div/span/small')
    show_on_menu_status = (By.XPATH, '//*[@id="displayInMenu"]')
    meta_description_input = (By.NAME, 'category.meta_description')
    meta_keywords_input = (By.NAME, 'category.meta_keywords')
    btn = (By.XPATH, '/html/body/div/div/section[2]/div/div[1]/div/form/div[2]/div/div/button')
    form =['title','pid','slug','order_number','content','summary','icon','flag','show_on_menu','meta_description','meta_keywords','order_number']

    new_category_slug =(By.TAG_NAME,'td')
    new_category_decription=(By.TAG_NAME,'td')
    new_category_order_number=(By.TAG_NAME,'td')

    toast= (By.XPATH, '//*[@id="toast-container"]/div/div[3]')
    toast_text = (By.CLASS_NAME,'toast-message')
    def __init__(self,driver,hostname):
        super().__init__(driver)
        self.base_url = f"http://{hostname}/jpress/admin/index".format(hostname=hostname)
        self.url = f"http://{hostname}/jpress/admin/article/category".format(hostname=hostname)
    def goto_category_page(self):
        self.goto_page(self.url)

    def fill_input(self, element_name, text):
        self.input_text(self.__getattribute__(element_name), text)

    def select_parent(self, parent):
        if parent:
            pid_select = Select(self.find_element(self.pid_input))
            pid_select.select_by_visible_text(parent)
    def show_on_menu_checked(self):
        return True if self.find_element(self.show_on_menu_status).get_property('value') == 'true' else False
    def click_show_on_menu(self):
        self.click(self.show_on_menu_input)
    def set_show_on_menu(self,status=False):
        if (not status and self.show_on_menu_checked()):
            self.click_show_on_menu()
    def fill_form(self,form_info:dict):
        '''
        fill the whole form
        :param form_info: dict of form_info to fill in
        :return: None
        '''
        for element in self.form:
            if element == 'pid':
                self.select_parent(form_info.get('pid',None))
            elif element == 'show_on_menu':
                status_to_set = form_info.get('show_on_menu',False)
                self.set_show_on_menu(status_to_set)
            else:
                self.fill_input(f'{element}_input'.format(element), form_info.get(element,''))
    def submit(self):
        self.click(self.btn)

    def wait_until_new_category_visibale(self,title,timeout=3,interval=0.5):
        self.new_category_title = (By.LINK_TEXT, title)
        self.wait_until_element_visibale(self.new_category_title)

    def wait_until_toast_present(self):
        self.wait_until_element_present(self.toast)
    def get_toast_text(self):
        return self.get_text(self.toast_text)


    def get_new_category_slug_element(self):
        elements = self.element_right_of(self.new_category_slug, self.new_category_title)
        return self.find_elements(elements)[0]


    def get_new_category_slug(self):
        attempts = 0
        while attempts < 2:
            try:
                return self.get_new_category_slug_element().text
            except StaleElementReferenceException:
                attempts += 1
























